package utilities;

import java.util.Random;
import java.util.Vector;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class CRes {
    //public static Image pointer;

    static Random r = new Random();
    
    /**
     * Slope calculation for line given by two x-y-positions
     *
     * @param x1 First position's x-parameter
     * @param y1 First position's y-parameter
     * @param x2 Second position's x-parameter
     * @param y2 Second position's y-parameter
     * @return calculated slope value
    */
    public static double calcLineSlope(int x1, int y1, int x2, int y2) {
      double dx = x1 - x2;
      double dy = y1 - y2;
      if ((dx != 0) && (dy != 0)) {
        return (dy / dx);
      } else {
        return 0;
      }
    }

    /**
     * Intercept calculation for line given by x-y-position and slope
     *
     * @param x x-parameter of given position
     * @param y y-parameter of given position
     * @param m slope of line
     * @return calculated intercept value
     */
    public static double calcLineIntercept(int x, int y, double m) {
      if (m != 0) {
        return y - m*x;
      } else {
        return 0;
      }
    }

    public static int random(int a, int b) {
        if (a >= b) {
            return a;
        }
        return a + (r.nextInt(b - a));
    }

    public static int random(int a) {
        return r.nextInt() % a;
    }

    public static void saveRMS(String filename, byte[] data) throws Exception {
        RecordStore rec = RecordStore.openRecordStore(filename, true);
        if (rec.getNumRecords() > 0) {
            rec.setRecord(1, data, 0, data.length);
        } else {
            rec.addRecord(data, 0, data.length);
        }
        rec.closeRecordStore();
    }
    
    public static void deleteRMS(String filename) throws RecordStoreException{
        RecordStore.deleteRecordStore(filename);
    }

    public static byte[] loadRMS(String filename) {
        RecordStore rec;
        byte[] data;
        try {
            rec = RecordStore.openRecordStore(filename, false);
            data = rec.getRecord(1);
            rec.closeRecordStore();
        } catch (Exception e) {
            return null;
        }
        return data;
    }

    public static void saveRMSString(String filename, String s) {
        try {
            saveRMS(filename, s.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String loadRMSString(String filename) {
        byte[] data = loadRMS(filename);
        if (data == null) {
            return null;
        } else {
            try {
                String s = new String(data, "UTF-8");
                return s;
            } catch (Exception e) {
                return new String(data);
            }
        }
    }

    public static void saveRMSInt(String file, int x) {
        try {
            saveRMS(file, new byte[]{(byte) x});
        } catch (Exception e) {
        }
    }

    public static int loadRMSInt(String file) {
        byte[] b = loadRMS(file);
        return b == null ? -1 : b[0];
    }
    
    public static void cleanVector(Vector vt){
    	if(vt != null){
    		vt.removeAllElements();
    		vt = null;
    	}
    }
}
